﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;
using System.Data;

namespace Ewts.Storage
{
    public partial class Authorization : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string username = "";
                int proid = 0;
                if (Session["UserInfo"] != null )
                {
                    User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (User)Session["UserInfo"];
                    username = curUser.UserName;
                    txtUserName.Value = username;
                    hidTname.Value = curUser.TrueName;
                }
                else
                {
                    Response.Redirect("/accounts/login.aspx");
                }
                //bindData(username);
            }

        }

        //protected void bindData(string username)
        //{
        //    DataLib.BLL.ProjectsRelation bll = new DataLib.BLL.ProjectsRelation();
        
        //    DataTable dt = bll.getAddressByUserName(username,2);
        //    rept.DataSource = dt;
        //    rept.DataBind();
        //}

        protected void rept_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "授权")
            {
                DataLib.Model.Cjo model = new DataLib.BLL.Cjo().GetModel(int.Parse(e.CommandArgument.ToString()));
                model.status = 4;
                if (new DataLib.BLL.Cjo().Update(model) == true)
                {
                    DataLib.Common.MessageBox.Show(Page, "授权成功");
                }
               // bindData(txtUserName.Value);
            }

            if (e.CommandName == "撤销")
            {
                DataLib.Model.Cjo model = new DataLib.BLL.Cjo().GetModel(int.Parse(e.CommandArgument.ToString()));
                model.status = -4;
                if (new DataLib.BLL.Cjo().Update(model) == true)
                {
                    DataLib.Common.MessageBox.Show(Page, "撤销已提交，请等待批复");
                }
               // bindData(txtUserName.Value);

            }
        }


        /// <summary>
        /// 获取按钮的状态
        /// </summary>
        /// <param name="status"></param>
        /// <param name="linktype"></param>
        /// <returns></returns>
        protected bool GetEnable(string status, int linktype)
        {
            bool strflag = false;
            if (status == "3")
            {
                //授权按钮
                if (linktype == 0)
                    strflag = true; 
 
            }
            if (status == "4")
            {
                if (linktype == 1)
                    strflag = true;
            }
            return strflag;
        }

        /// <summary>
        /// 取得当前状态的中文描述
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        protected string GetStatus(string status)
        {
            string statusname = "";
            switch (status)
            {
                case "1":
                    statusname = "待派车";
                    break;
                case "2":
                    statusname = "待称重";
                    break;
                case "3":
                    statusname = "待授权";
                    break;
                case "4":
                    statusname = "待拆解";
                    break;
                case "5":
                    statusname = "拆解中";
                    break;
                case "-4":
                    statusname = "拆解申请审批中";
                    break;
                default:
                    break;
            }
            return statusname;

        }

        protected void rept_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            LinkButton link1 = (LinkButton)e.Item.FindControl("linkAuth");
            if (link1 != null)
            {
                if (link1.Enabled == true)
                {
                    link1.ForeColor = System.Drawing.Color.Blue; ;
                }
                else
                {
                    link1.ForeColor = System.Drawing.Color.Gray;
                }
            }
            LinkButton link2 = (LinkButton)e.Item.FindControl("linkRevoke");
            if (link2 != null)
            {
                if (link2.Enabled == true)
                {
                    link2.ForeColor = System.Drawing.Color.Blue ;
                }
                else
                {
                    link2.ForeColor = System.Drawing.Color.Gray;                }
            }
        }
    }
}